﻿Public Class MASSUBDISTRICT
    Dim connect As CONDB = CONDB.NewConnection()
    'Dim connectlocal As conlocal = conlocal.NewConnection
    Dim PROVIDER_MASSUBDISTRICT As New PROVIDERSCLASS

    Private Sub codeTambonTextbox_KeyPress(sender As Object, e As KeyPressEventArgs)
        If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = True
        End If
    End Sub

    Private Sub filterCountry_TextChanged(sender As Object, e As EventArgs) Handles filterCountry.TextChanged
        If filterCountry.Text.ToString.Length > 0 Then
            filterProvince.Enabled = True
            Dim s2 As FILTERCLASS
            Dim sql2 As String = "SELECT CAST(codechangwat AS CHAR(8)) AS CODECHANGWAT, changwat FROM maschangwat WHERE codecountry1 = '" & filterCountry.Tag & "'"
            s2 = New FILTERCLASS(filterProvince, sql2, "รหัสจังหวัด,ชื่อจังหวัด", "50,160", "1,1", "1,1")
            's.SetShowInDown = False
            s2.SetShowBorder = True
        Else
            filterProvince.Enabled = False
        End If
    End Sub

    Private Sub filterProvince_TextChanged(sender As Object, e As EventArgs) Handles filterProvince.TextChanged
        If filterProvince.Text.ToString.Length > 0 Then
            filterDistrict.Enabled = True
            Dim s3 As FILTERCLASS
            Dim sql3 As String = "SELECT CAST(ampid AS CHAR(8)) AS AMPID, ampur, CAST(codeampur AS CHAR(8)) AS CODEAMPUR FROM masampur WHERE codechangwat = '" & filterProvince.Tag & "'"
            s3 = New FILTERCLASS(filterDistrict, sql3, "IDอำเภอ,ชื่ออำเภอ,รหัสอำเภอ", "50,160,50", "1,1,1", "1,1,1")
            's.SetShowInDown = False
            s3.SetShowBorder = True
        Else
            filterDistrict.Enabled = False
        End If
    End Sub

    Private Sub filterCountry_Click(sender As Object, e As EventArgs) Handles filterCountry.Click
        filterCountry.Text = ""
        filterCountry.Tag = ""
    End Sub

    Private Sub filterProvince_Click(sender As Object, e As EventArgs) Handles filterProvince.Click
        filterProvince.Text = ""
        filterProvince.Tag = ""
    End Sub

    Private Sub filterDistrict_Click(sender As Object, e As EventArgs) Handles filterDistrict.Click
        filterDistrict.Text = ""
        filterDistrict.Tag = ""
    End Sub

    Private Sub MASSUBDISTRICT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        selectDataGridTambon()

        Dim s As FILTERCLASS
        Dim sql As String = "SELECT CAST(codecountry AS CHAR(8)) AS CODECOUNTRY, country FROM mascountry"
        s = New FILTERCLASS(filterCountry, sql, "รหัสประเทศ,ชื่อประเทศ", "50,160", "1,1", "1,1")
        's.SetShowInDown = False
        s.SetShowBorder = True

        DesignForm(Me)
        DesignGrid(MASTAMBONGrid)

        PROVIDER_MASSUBDISTRICT.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))

        connect.Dispose()
        'connectlocal.Dispose()
    End Sub

    Private Sub selectDataGridTambon()
        Dim objSelectDatamasTambon As New prated
        Dim MASTAMBON As DataTable
        MASTAMBON = connect.GetTable(objSelectDatamasTambon.selectTambon_)
        MASTAMBONGrid.PrimaryGrid.DataSource = MASTAMBON
    End Sub

    Private Sub MASTAMBONGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASTAMBONGrid.CellClick
        Dim grid As DevComponents.DotNetBar.SuperGrid.GridRow
        grid = e.GridPanel.ActiveRow
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TMBID").Value() IsNot DBNull.Value Then
            TMBIDTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("TMBID").Value()
        Else
            TMBIDTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECOUNTRY").Value() IsNot DBNull.Value Then
            filterCountry.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ประเทศ").Value()
            filterCountry.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECOUNTRY").Value()
        Else
            filterCountry.Text = ""
            filterCountry.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECHANGWAT").Value() IsNot DBNull.Value Then
            filterProvince.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("จังหวัด").Value()
            filterProvince.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("CODECHANGWAT").Value()
        Else
            filterProvince.Text = ""
            filterProvince.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("AMPID").Value() IsNot DBNull.Value Then
            filterDistrict.Tag = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("AMPID").Value()
            filterDistrict.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("อำเภอ").Value()
        Else
            filterDistrict.Text = ""
            filterDistrict.Tag = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำบล").Value() IsNot DBNull.Value Then
            TAMBONTxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ตำบล").Value()
        Else
            TAMBONTxt.Text = ""
        End If
        If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสไปรษณีย์").Value() IsNot DBNull.Value Then
            POSTCODETxt.Text = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสไปรษณีย์").Value()
        Else
            POSTCODETxt.Text = ""
        End If
        addTambon.Enabled = False
    End Sub

    Private Sub addTambon_Click_1(sender As Object, e As EventArgs) Handles addTambon.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASSUBDISTRICT.PADD_ = True Then
                If filterCountry.Tag.ToString.Length > 0 And filterProvince.Tag.ToString.Length > 0 And filterDistrict.Tag.ToString.Length > 0 And TAMBONTxt.Text.Length > 0 Then
                    'connectlocal = conlocal.NewConnection
                    Dim objInsertData As New prated
                    'objInsertData.tambonTMBID_ = CInt(codeTambonTextbox.Text)
                    objInsertData.tambonAMPID_ = filterDistrict.Tag
                    objInsertData.tambonCODECHANGWAT_ = filterProvince.Tag
                    objInsertData.tambonCODECOUNTRY_ = filterCountry.Tag
                    objInsertData.tambonTAMBON_ = TAMBONTxt.Text
                    objInsertData.tambonPostcode_ = POSTCODETxt.Text
                    objInsertData.insertTambon()

                    selectDataGridTambon()

                    TMBIDTxt.Text = ""
                    TAMBONTxt.Text = ""
                    POSTCODETxt.Text = ""
                    filterCountry.Text = ""
                    filterCountry.Tag = ""
                    filterProvince.Text = ""
                    filterProvince.Tag = ""
                    filterDistrict.Text = ""
                    filterDistrict.Tag = ""
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการเพิ่มข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_MASSUBDISTRICT.PADDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editTambon_Click_1(sender As Object, e As EventArgs) Handles editTambon.Click
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_MASSUBDISTRICT.PUPD_ = True Then
                If filterCountry.Tag.ToString.Length > 0 And filterProvince.Tag.ToString.Length > 0 And filterDistrict.Tag.ToString.Length > 0 And TAMBONTxt.Text.Length > 0 Then
                    Dim editStat As Integer
                    editStat = MsgBox("ท่านต้องการแก้ไขตำบลดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)

                    If editStat = 1 Then
                        'connectlocal = conlocal.NewConnection
                        Dim objEditData As New prated
                        objEditData.tambonTMBID_ = TMBIDTxt.Text
                        objEditData.tambonAMPID_ = filterDistrict.Tag
                        objEditData.tambonCODECHANGWAT_ = filterProvince.Tag
                        objEditData.tambonCODECOUNTRY_ = filterCountry.Tag
                        objEditData.tambonTAMBON_ = TAMBONTxt.Text
                        objEditData.tambonPostcode_ = POSTCODETxt.Text
                        objEditData.editTambon()

                        selectDataGridTambon()

                        TMBIDTxt.Text = ""
                        TAMBONTxt.Text = ""
                        POSTCODETxt.Text = ""
                        filterCountry.Text = ""
                        filterCountry.Tag = ""
                        filterProvince.Text = ""
                        filterProvince.Tag = ""
                        filterDistrict.Text = ""
                        filterDistrict.Tag = ""
                    End If
                Else
                    MsgBox("ท่านต้องทำการกรอกข้อมูลให้ครบถ้วนก่อนทำการแก้ไขข้อมูล")
                End If
            Else
                MsgBox(PROVIDER_MASSUBDISTRICT.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub importExel_Click(sender As Object, e As EventArgs) Handles importExel.Click
        Dim nextform As New IMPORTEXCEL(mastambon.Text)
        nextform.ShowDialog()
    End Sub

    Private Sub ButtonItem1_Click(sender As Object, e As EventArgs) Handles ButtonItem1.Click
        Dim nextform As New IMPORTEXCEL(mastambon.Text)
        nextform.ShowDialog()
    End Sub
End Class